package com.ktjy.springredis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.springredis.entity.Right;
import com.ktjy.springredis.entity.Role;
import com.ktjy.springredis.entity.User;
import com.ktjy.springredis.service.IRoleService;
import com.ktjy.springredis.service.RightService;
import com.ktjy.springredis.service.RoleRightService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @return 13141
 * 2025/7/18
 * 09:25
 */
@Controller
public class RoleController {
    @Resource
    private IRoleService roleService;
    @Resource
    private RightService rightService;
    @Resource
    private RoleRightService roleRightService;

    private static final Logger LOGGER = LoggerFactory.getLogger(RoleController.class);

    @RequestMapping("/role/list")
    public String list(Model model,
                       String roleName,
                       @RequestParam(required = false, defaultValue = "1") int pageIndex) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("roleName", roleName);
        hashMap.put("pageIndex", pageIndex);
        hashMap.put("pageSize", 3);
        IPage<Role> roleIPage = roleService.getRoleByPage(hashMap);
        model.addAttribute("rolePage", roleIPage);
        model.addAttribute("roleName", roleName);
        return "/role/list";
    }

    // 跳转新增页面
    @GetMapping("/role/add")
    public String toAdd(Model model) {
        // 查询所有角色
        List<Right> rights = rightService.findAll();
        model.addAttribute("rights", rights);
        return "role/add";
    }

    // 保存角色信息到数据库
    @PostMapping("/role/save")
    public String add(Role role) {
        int result = roleService.insert(role);
        if (result > 0) {
            return "redirect:/role/list";
        } else {
            return "redirect:/role/add";
        }
    }

    // 跳转到修改页面
    @GetMapping("/role/edit")
    public String toEdit(Long roleId, Model model) {
        Role role = roleService.get(roleId);
        List<Right> rights = rightService.findAll();
        List<Right> roleRights = roleRightService.getRoleRights(roleId);
        model.addAttribute("role", role);
        model.addAttribute("rights", rights);
        model.addAttribute("roleRights", roleRights);
        return "/role/edit";
    }

    // 更新角色信息到数据库
    @PostMapping("/role/edit")
    public String edit(Role role) {
        int result = roleService.update(role);
        if (result > 0) {
            return "redirect:/role/list";
        } else {
            return "redirect:/role/edit";
        }
    }

    // 用户删除
    @RequestMapping("/role/del")
    @ResponseBody
    public Map del(Long roleId) {
        int result = roleService.delete(roleId);
        Map map = new HashMap();
        if (result > 0) {
            map.put("delResult", true);
        } else {
            map.put("delResult", false);
        }
        return map;
    }
}
